package com.alibaba.sample.petstore.dal.dao.cached;

import java.util.List;
import java.util.Map;

import com.alibaba.sample.petstore.dal.dao.UserDao;
import com.alibaba.sample.petstore.dal.dataobject.User;

public class CachedUserDao extends AbstractCachedDao<UserDao> implements UserDao {
    private final Map<String, User> usersById = createCache();
    private List userIdList;

    public CachedUserDao(UserDao backingDao) {
        super(backingDao);
    }

    public User getAuthenticatedUser(String userId, String password) {
        return backingDao.getAuthenticatedUser(userId, password);
    }

    public User getUserById(String userId) {
        User user = usersById.get(userId);

        if (user == null) {
            user = backingDao.getUserById(userId);
            usersById.put(userId, user);
        }

        return user;
    }

    public List getUserIdList() {
        if (userIdList == null) {
            userIdList = backingDao.getUserIdList();
        }

        return userIdList;
    }

    public void insertUser(User user) {
        backingDao.insertUser(user);
    }

    public void updateUser(User user) {
        backingDao.updateUser(user);
    }
}
